define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wx/wxorder/index',
                    add_url: 'wx/wxorder/add',
                    edit_url: 'wx/wxorder/edit',
                    del_url: 'wx/wxorder/del',
                    multi_url: 'wx/wxorder/multi',
                    import_url: 'wx/wxorder/import',
                    finance_url: 'wx/wxorder/finance',
                    importwlx_url: 'wx/wxorder/importwlx',//下册快递单号导入
                    importwl_url: 'wx/wxorder/importwl',//快递单号导入
                    table: 'wxorder',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true,formatter:function (value,row,index) {
                            if(row.state == 1)
                            {
                              return {
                                disabled: true
                              };
                            }
                          }},
                        {field: 'id', title: __('Id')},
                        {field: 'status', title: __('订单状态'),formatter:function (value,row,index) {
                            if(row.status == 1 || row.status == 3)
                            {
                            return '<span class="btn btn-success">'+row.status_text+'</span>';
                            }
                            if(row.status == 2 || row.status == 4)
                            {
                              return '<span class="btn  btn-warning">'+row.status_text+'</span>';
                            }
                            return '<span class="btn">'+row.status_text+'</span>';
                          }},
                        {field: 'state', title: __('审核状态'),searchList: {"0":__('未审核'),"1":__('审核通过')},formatter: function (value,row,index) {
                            return '<span class="btn btn-primary">'+row.state_text+'</span>';
                        }},
                        {field: 'info.agent_name', title: __('Info.agent_name')},
                        {field: 'order_sn', title: __('Order_sn')},
                        {field: 'xdtime', title: __('Xdtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'gifts', title: __('Gifts')},
                        {field: 'akg', title: __('重量')},
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'receipt', title: __('Receipt')},
                        {field: 'phone', title: __('Phone')},
                        {field: 'address', title: __('Address')},
                        {field: 'orderStatus', title: __('发货状态'),searchList: {"0":__('待发货'),"1":__('已发货')},formatter:function (value,row,index) {
                            if((row.status == 0 || row.status == 1 || row.status == 3) && row.orderStatus == 0)
                            {
                              return '<span class="btn btn-success">待发货</span>';
                            }
                            if(row.orderStatus == 1)
                            {
                              return '<span class="btn  btn-warning">已发货</span>';
                            }
                            return '<span class="btn">-</span>';
                          }},
                        {field: 'deliveryForm', title: __('快递方式')},
                        {field: 'courierBut', title: __('快递单号')},
                        //启用时间段搜索
                        {
                          field: 'createtime',
                          title: __('创建时间'),
                          sortable: true,
                          formatter: Table.api.formatter.datetime,
                          operate: 'RANGE',
                          addclass: 'datetimerange'
                        },
                        {
                          field: 'updatetime',
                          title: __('更新时间'),
                          sortable: true,
                          formatter: Table.api.formatter.datetime,
                          operate: 'RANGE',
                          addclass: 'datetimerange'
                        },
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                        //操作栏,默认有编辑、删除或排序按钮,可自定义配置buttons来扩展按钮
                        {
                          field: 'operate',
                          width: "150px",
                          title: __('Operate'),
                          table: table,
                          events: Table.api.events.operate,
                          buttons: [
                            {
                              name: '审核',
                              icon: 'fa fa-bell-o fa-lg',
                              title: __('审核'),
                              extend: 'data-toggle="tooltip" data-params="createtime="',
                              classname: 'btn btn-xs btn-info btn-finance',
                              hidden:function (row) {
                                if(row.state == 1)
                                {
                                  return true;
                                }
                                return false;
                              }
                            },
                            {
                              name: 'detail',
                              title: __('订单详情查看'),
                              classname: 'btn btn-xs btn-primary btn-dialog',
                              icon: 'fa fa-list',
                              url: 'wx/wxorderinfo/index',
                            }
                          ],
                          formatter: Table.api.formatter.operate
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

          //导入
          require(['upload'], function (Upload) {

            //物流单号导入
            Upload.api.plupload($('.btn-importwlx'), function (data, ret) {
              Fast.api.ajax({
                url:$.fn.bootstrapTable.defaults.extend.importwlx_url ,
                data: {file: data.url},
              }, function (data, ret) {
                if(data.length >= 1){
                  var htmlstr = '';
                  data.forEach(function (element, index, array) {
                    htmlstr +='<li>'+array[index]+' </li>';
                  });
                  layer.open({
                    type: 1,
                    title: '错误的数据',
                    skin: 'layui-layer-demo', //样式类名
                    closeBtn: 1, //不显示关闭按钮
                    anim: 2,
                    shadeClose: true, //开启遮罩关闭
                    content: '<ul>'+htmlstr+'</ul>'
                  });
                }
                table.bootstrapTable('refresh');
              });
            });
            //物流单号导入 下册
            Upload.api.plupload($('.btn-importwl'), function (data, ret) {
              Fast.api.ajax({
                url: $.fn.bootstrapTable.defaults.extend.importwl_url,
                data: {file: data.url},
              }, function (data, ret) {
                if(data.length >= 1){
                  var htmlstr = '';
                  data.forEach(function (element, index, array) {
                    htmlstr +='<li>'+array[index]+' </li>';
                  });
                  layer.open({
                    type: 1,
                    title: '错误的数据',
                    skin: 'layui-layer-demo', //样式类名
                    closeBtn: 1, //不显示关闭按钮
                    anim: 2,
                    shadeClose: true, //开启遮罩关闭
                    content: '<ul>'+htmlstr+'</ul>'
                  });
                }
                table.bootstrapTable('refresh');
              });
            });
          });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});